<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_rss_Item
{
    /* @var $element f_xml_XmlElement */
    private $element;

    private $nameSpaces;

    private $twitterUser;

    private function __construct($element, $twitterUser = null)
    {
        $this->twitterUser = $twitterUser;
        $this->element = $element;
        $this->nameSpaces = $element->getNameSpaces(true);
    }

    public static function getInstance($element, $twitterUser = null)
    {
        return new self($element, $twitterUser);
    }

    public function getTitle()
    {
        return $this->element->getSubElement('title')->getContent();
    }

    public function getHtmlContent()
    {
        $content = '';
        if(isset($this->nameSpaces['content']))
        {
            $content = (string) $this->element->children($this->nameSpaces['content']);
        }
        return $content;
    }

    public function getDescription()
    {
        return $this->element->getSubElement('description')->getContent();
    }

    public function getElapsedTime()
    {
        return f_date_DateFormat::timeSince($this->getDateObject());
    }

    public function getTweet()
    {
        $tweet = substr($this->element->getSubElement('description')->getContent(), strlen($this->twitterUser)+2);
        $tweet = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.-]*(\?\S+)?(#\S+)?)?)?)@','<a href="$1">$1</a>', $tweet);
        $tweet = preg_replace('/@(\w+)/','@<a href="http://twitter.com/$1">$1</a>', $tweet);
        $tweet = preg_replace('/\s+#(\w+)/', ' <a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $tweet);
        return $tweet;
    }

    public function getPubDate()
    {
        return $this->element->getSubElement('pubDate')->getContent();
    }

    private function getDateObject()
    {
        $date = date(DATE_RSS, strtotime($this->element->getSubElement('pubDate')->getContent()));
        return f_date_Date::getInstanceFromFormat($date, DATE_RFC822);
    }

    public function getPubDateFormatted()
    {
        return f_date_DateFormat::smartFormatByLang($this->getDateObject(), 'localized-user-time-format');
    }

    public function getLink()
    {
        return $this->element->getSubElement('link')->getContent();
    }

    public function getImages()
    {
        $content = $this->getHtmlContent();

        if($content == '') $content = $this->getDescription();

        return f_utils_String::getImgSrcFromTags($content);
    }

    public function getMainImage($minWidth = 100, $minHeight = 100)
    {
        $images = $this->getImages();

        if(is_array($images) && count($images) > 0)
        {
            foreach($images as $image)
            {
                list($width, $height, $type, $attr) = getimagesize($image);

                if($width >= $minWidth && $height >= $minHeight)
                {
                    return $image;
                }
            }
        }

        return '';
    }

    private function getNameSpaces()
    {
        return $this->element->getNameSpaces(true);
    }
}